package com.anng.hosp.controller.api;

import com.anng.common.exception.YyghException;
import com.anng.common.helper.HttpRequestHelper;
import com.anng.common.result.Result;
import com.anng.common.result.ResultCodeEnum;
import com.anng.common.utils.MD5;
import com.anng.hosp.model.hosp.Department;
import com.anng.hosp.model.hosp.Hospital;
import com.anng.hosp.model.hosp.Schedule;
import com.anng.hosp.service.DepartmentService;
import com.anng.hosp.service.HospitalService;
import com.anng.hosp.service.HospitalSetService;
import com.anng.hosp.service.ScheduleService;
import com.anng.hosp.vo.hosp.DepartmentQueryVo;
import com.anng.hosp.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //工具类签名检验
    public String util(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String,String[]> requestMap=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //1.获取医院系统转递过来的签名，签名进行MD5加密
        String hospSign =(String) paramMap.get("sign");
        //2.根据转递过来的医院编号，查询数据库，查询签名
        String hoscode =(String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4.判断签名是否一致
        if(!hospSign.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        return hoscode;
    }

    //删除排班接口
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        //获取传过来的排班信息
        Map<String,String[]> requestMap=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院的编号 和排班编号
        String hoscode=(String) paramMap.get("hoscode");
        String hosScheduleId=(String) paramMap.get("hosScheduleId");
        //签名检验
        util(request);
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }


    //查询排班
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取传过来的排班信息
        Map<String,String[]> requestMap=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院的编号
        String hoscode=(String) paramMap.get("hoscode");
        //获取科室的编号
        //String depcode=(String) paramMap.get("depcode");
        //获取当前页和每页记录数
        int page= StringUtils.isEmpty(paramMap.get("page"))
                ?1:Integer.parseInt ((String)paramMap.get("page"));
        int limit= StringUtils.isEmpty(paramMap.get("limit"))
                ?1:Integer.parseInt ((String)paramMap.get("limit"));

        //签名检验
        util(request);

        ScheduleQueryVo scheduleQueryVo =new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);

        //调用service 方法
        Page<Schedule> pageMode= scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageMode);
    }

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传过来的排班信息
        Map<String,String[]> requestMap=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //签名检验
        util(request);
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传过来的科室信息
        Map<String,String[]> requestMap=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院的编号 和科室编号
        String hoscode=(String) paramMap.get("hoscode");
        String depcode=(String) paramMap.get("depcode");
        //签名检验
        util(request);
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }


    //查询科室接口
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取传过来的科室信息
        Map<String,String[]> requestMap=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院的编号
        String hoscode=(String) paramMap.get("hoscode");
        //获取当前页和每页记录数
        int page= StringUtils.isEmpty(paramMap.get("page"))
                ?1:Integer.parseInt ((String)paramMap.get("page"));
        int limit= StringUtils.isEmpty(paramMap.get("limit"))
                ?1:Integer.parseInt ((String)paramMap.get("limit"));
        //签名检验
        util(request);
        DepartmentQueryVo departmentQueryVo =new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service 方法
        Page<Department> pageMode= departmentService.findPageDepaartment(page,limit,departmentQueryVo);
        return Result.ok(pageMode);
    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String,String[]> requestMap=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        util(request);
        //调用service中的方法
        departmentService.seva(paramMap);
        return Result.ok();

    }

    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        String hoscode=util(request);
        //调用service的方法实现根据医院编号查询
        Hospital hospital =hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);

    }
    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        Map<String,String[]> requestMap=request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        util(request);

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String)paramMap.get("logoData");
        String logoData = logoDataString.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }
}
